import {
	defaultUrl
} from './defaultUrl.js';
// index 为了确保都加载成功,再关闭下拉刷新
let index = 0;

function fn(config) {
	const token = uni.getStorageSync('token');
	index++;
	uni.showLoading({
		title: '加载中'
	})
	return new Promise((resolve, reject) => {
		const data = {
			url: `${defaultUrl}${config.url}`,
			data: config.data,
			method: config.method ? config.method : "GET",
			timeout: 6000,
			success: (res) => {
				index--;
				resolve(res)
				if (index == 0) {
					uni.stopPullDownRefresh();
					uni.hideLoading()
				}
			},
			fail() {
				uni.showToast({
					title: "请求失败"
				})
			}

		}
		if (token) {
			data.header = config.header = {
				token
			}
		}
		uni.request(data);
	})
}

export default fn;
